//
//  OrderRecordManager.m
//  Order
//
//  Created by funny on 12-3-24.
//  Copyright 2012 __MyCompanyName__. All rights reserved.
//

#import "OrderRecordManager.h"

#import <PlausibleDatabase/PlausibleDatabase.h>

@implementation OrderRecordManager

static OrderRecordManager *sharedOrderRecordManager = nil;

- (NSString *)recordPath{
	NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
	NSString *recordPath = [documentPath stringByAppendingPathComponent:@"OrderRecord.sqlite"];
	
	return recordPath;
}

- (id)init{
    if (self = [super init])  {
		NSString *recordPath = [self recordPath];
		
		NSFileManager *fileManager = [NSFileManager defaultManager];
		if (![fileManager fileExistsAtPath:recordPath]) {
			sqlite3 *database;
			
			if (sqlite3_open([recordPath UTF8String], &database) != SQLITE_OK) {
				sqlite3_close(database);
				NSAssert(0, @"Failed to open database");
			}
			
			char *errorMsg;
			NSString *createSQL = @"CREATE TABLE IF NOT EXISTS order_record (id INTEGER PRIMARY KEY, clerk_num INTEGER, desk_num INTEGER, time INTEGER, order_content TEXT);";
			if (sqlite3_exec (database, [createSQL  UTF8String], NULL, NULL, &errorMsg) != SQLITE_OK) {
				sqlite3_close(database);
				NSAssert1(0, @"Error creating table: %s", errorMsg);
			}
			
			sqlite3_close(database);
		}
	}
	return self;
}

+ (OrderRecordManager *)sharedOrderRecordManager{
    @synchronized(self){
		if  (sharedOrderRecordManager == nil){
            [[self alloc] init];
        }
    }
	
	return  sharedOrderRecordManager;
}

+ (id)allocWithZone:(NSZone *)zone{
    @synchronized(self){
		if (sharedOrderRecordManager == nil){
            sharedOrderRecordManager = [super allocWithZone:zone];
			
			return  sharedOrderRecordManager;
        }
    }
	
	return  nil;
}

- (BOOL)insertOrderRecord:(OrderRecordDB *)newRecord{
	NSString *recordPath = [self recordPath];
	
	PLSqliteDatabase *database = [[PLSqliteDatabase alloc] initWithPath:recordPath];
	[database open];
	
	return [database executeUpdate: @"INSERT INTO FIELDS (clerk_num, desk_num, time, order_content) VALUES (?, ?, ?, ?)", @"aa", @"bb", @"cc", @"dd"];
}

- (NSArray *)allOrderRecord{
	NSString *recordPath = [self recordPath];
	
	PLSqliteDatabase *database = [[PLSqliteDatabase alloc] initWithPath:recordPath];
	[database open];
	
	id<PLResultSet> rs;
	
	NSString *findSql = [NSString stringWithFormat:@"select * FROM FIELDS"];
	rs = [database executeQuery:findSql];
	
	while([rs next]) {
		NSString *row  = [rs objectForColumn:@"ROW"];
		NSString *data = [rs objectForColumn:@"FIELD_DATA"];
		
		NSLog(@"row : %@\ndata : %@", row, data);
	}
	
	[rs close];
	
	[database close];
	
	return [[[NSArray alloc] init] autorelease];
}

- (NSArray *)todayOrderRecord{
	NSString *recordPath = [self recordPath];
	
	PLSqliteDatabase *database = [[PLSqliteDatabase alloc] initWithPath:recordPath];
	[database open];
	
	id<PLResultSet> rs;
	
	NSString *findSql = [NSString stringWithFormat:@"select * FROM FIELDS"];
	rs = [database executeQuery:findSql];
	
	while([rs next]) {
		NSString *row  = [rs objectForColumn:@"ROW"];
		NSString *data = [rs objectForColumn:@"FIELD_DATA"];
		
		NSLog(@"row : %@\ndata : %@", row, data);
	}
	
	[rs close];
	
	[database close];
	
	return [[[NSArray alloc] init] autorelease];
}

- (void)dealloc {
    [super dealloc];
}

@end
